package main
import (
	"fmt"
	// "math/rand"
)

/*
1)启动一个协程，将1-2000的数放入到一个channel中,比如numChan
2)启动8个协程 ，从numChan取出数(比如n)， 并计算1+..+n的值，并
存放到resChan
3)最后8个协程协同完成工作后，再遍历resChan,显示结果[如res[1]= 1.. res[10]=55 .]」
4) 注意:考虑resChan chan int是否合适?
*/
var resChan = make(chan int ,200000)


func createData(numChan chan int){
	
	for i:=1;i<=200;i++{
		numChan <- i
	}
	close(numChan)

}

func CalData(numChan chan int,exitChan chan bool){
	
	for v := range numChan{
		res :=0
		for a:=1;a<=v;a++{
			res += a
		}
		resChan <- res
	}
	exitChan <- true
	// close(exitChan)

}


func main() {
	var numChan chan int = make(chan int,2000)
	go createData(numChan)
	exitChan := make(chan bool, 10)
	for i:=0;i<8;i++{
		go CalData(numChan,exitChan)
	}

	go func(){
		for{
			if len(exitChan) == 8{
				fmt.Println("res exitChan-",len(exitChan))
				close(exitChan)
				close(resChan)
				break
			}
		}
	}()

	index :=1
	for v := range resChan{
		fmt.Printf("res[%v]=%v\n",index,v)
		index ++

	}
}

